package com.summer.safe.base;

import com.summer.safe.config.result.ResultCode;
import com.summer.safe.exception.CustomException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service基类
 *
 * @author
 */
@Transactional(readOnly = true, rollbackFor = Exception.class)
public abstract class BaseService<D extends BaseDao<T>, T extends BaseEntity<T>> {
    /**
     * 日志对象
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 持久层对象
     */
    @Autowired
    protected D dao;

    /**
     * 获取单条数据
     *
     * @param id
     * @return
     */
    public T get(String id) {
        return dao.get(id);
    }

    /**
     * 获取单条数据
     *
     * @param entity
     * @return
     */
    public T get(T entity) {
        return dao.get(entity);
    }

    /**
     * 查询列表数据
     *
     * @param entity
     * @return
     */
    public List<T> findList(T entity) {
        try {
            return dao.findList(entity);
        }catch ( Exception e){
            logger.error("抛出的异常{}", e.getMessage());
            throw new CustomException(ResultCode.FAIL_QUERY);
        }
    }



    /**
     * 保存数据（插入）
     *
     * @param entity
     */
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    public void save(T entity) {
        if (entity.getIsNewRecord()) {
            entity.preInsert();
            try {
                dao.insert(entity);
            }catch ( Exception e){
                logger.error("抛出的异常{}", e.getMessage());
                throw new CustomException(ResultCode.FAIL_SAVE);
            }

        } else {
            entity.preUpdate();
            try {
                dao.update(entity);
            }catch ( Exception e){
                logger.error("抛出的异常{}", e.getMessage());
                throw new CustomException(ResultCode.FAIL_SAVE);
            }
        }
    }



    /**
     * 更新数据
     *
     * @param entity
     */
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    public void update(T entity)  {
        try {
            dao.update(entity);
        }catch ( Exception e){
            logger.error("抛出的异常{}", e.getMessage());
            throw new CustomException(ResultCode.FAIL_SAVE);
        }

    }


    /**
     * 删除数据
     *
     * @param entity
     */
    @Transactional(readOnly = false)
    public void delete(T entity) {
        try {
            dao.delete(entity);
        }catch ( Exception e){
            logger.error("抛出的异常{}", e.getMessage());
            throw new CustomException(ResultCode.FAIL_DELETE);
        }
    }

    /**
     * 查询分页数据
     *
     * @param page   分页对象
     * @param entity
     * @return
     */
    public Page<T> findPage(Page<T> page, T entity) {
            page.setSort(entity.getSqlSort());
            page.setSortOrder(entity.getSortOrder());
            page.setList(dao.findList(entity));
            return page;

    }

}
